package com.github.prontera.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class SerialNumUtils {
	/**
	 * 获取现在时间
	 * 
	 * @return返回字符串格式yyyyMMddHHmmss
	 */
	public static String getStringDate() {
		Date currentTime = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
		String dateString = formatter.format(currentTime);
		return dateString;
	}

	/**
	 * 由年月日时分秒+3位随机数 生成流水号
	 * 
	 * @return
	 */
	public static String getNum() {
		String t = getStringDate();
		int x = (int) (Math.random() * 900) + 100;
		String serial = t + x;
		return serial;
	}

	public static String createNum(int max, Map<String, String> containsMap) {
		int x = (int) (Math.random() * max);
		String str = String.format("%04d", x);
		if (containsMap.containsKey(str)) {
			return createNum(max, containsMap);
		}
		return str;
	}

	// 主方法测试
	public static void main(String[] args) {

		Map<String, String> containsMap = new HashMap<>();
		containsMap.put("0051", "0051");
		int max = 100;
		for (int i = 0; i < 1000; i++) {
			System.out.println(createNum(max, containsMap));
		}

	}

}
